<!DOCTYPE html>
<script src="../../../../../resources/js-test.js"></script>
<style>
    body {
        height: 2000px;
        width: 2000px;
    }
</style>

<script>
window.jsTestIsAsync = true;

description("This test ensures that input driven user scrolls get cancelled\
    by non-input driven scrolls.");

// From ScrollAnimatorCompositorCoordinator::RunState.
var IDLE = "Idle";
var RUNNING_ON_COMPOSITOR = "RunningOnCompositor";
var WAITING_TO_CANCEL_ON_COMPOSITOR = "WaitingToCancelOnCompositor";
function isOrWillBeCancelled(node) {
    var state = internals.getScrollAnimationState(node);
    return state == WAITING_TO_CANCEL_ON_COMPOSITOR || state == IDLE;
}

function runTest() {
    if (document.scrollingElement.scrollTop == 0) {
        requestAnimationFrame(runTest);
    } else {
        shouldBe("internals.getScrollAnimationState(document)",
            "RUNNING_ON_COMPOSITOR");

        // Programmatic scroll should cancel smooth scroll.
        document.scrollingElement.scrollBy({top: 1, behavior: "smooth"});
        shouldBeTrue("isOrWillBeCancelled(document)");
        shouldBecomeEqual("document.scrollingElement.scrollTop < 40 && " +
          "document.scrollingElement.scrollLeft == 0", "true", finishJSTest);
    }
}

onload = function() {
    if (!window.eventSender || !window.internals) {
        finishJSTest();
        return;
    }

    // Turn on smooth scrolling.
    internals.settings.setScrollAnimatorEnabled(true);

    document.scrollingElement.scrollTop = 0;

    // Scroll down 1 tick (40px).
    eventSender.mouseMoveTo(20, 20);
    eventSender.mouseScrollBy(0, -1);

    requestAnimationFrame(runTest);
}
</script>
